package cn.epsilon3.mybatis.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import cn.epsilon3.constant.Base;
import cn.epsilon3.domain.e3.po.E3BlackIpPo;
import cn.epsilon3.mybatis.mapper.E3BlackIpMapper;
import cn.epsilon3.mybatis.service.E3BlackIpMapperService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

;

/**
 * <p>
 * 黑名单ip Mapper 服务
 * </p>
 *
 * @author epsilon3
 * @since 2023-08-16
 */
@Service
public class E3BlackIpMapperServiceImpl implements E3BlackIpMapperService {

    @Resource
    private E3BlackIpMapper blackIpMapper;

    @Override
    @Transactional(readOnly = true)
    public List<E3BlackIpPo> selectList(Integer isDelete) {
        LambdaQueryWrapper<E3BlackIpPo> e3BlackIpPoLambdaQueryWrapper = Wrappers.<E3BlackIpPo>lambdaQuery();
        if (ObjectUtil.isNotNull(isDelete)) {
            e3BlackIpPoLambdaQueryWrapper.eq(E3BlackIpPo::getEffective,isDelete);
        }
        List<E3BlackIpPo> blackIpPoList = blackIpMapper.selectList(e3BlackIpPoLambdaQueryWrapper);
        if (ObjectUtil.isNull(blackIpPoList) || Base.INT0.equals(blackIpPoList.size())) {
            return null;
        }
        return blackIpPoList;
    }

    @Override
    @Transactional(readOnly = true)
    public E3BlackIpPo selectOne(String ip) {
        if (StrUtil.isBlank(ip)) {
            return null;
        }
        return blackIpMapper.selectOne(Wrappers.<E3BlackIpPo>lambdaQuery()
                .eq(E3BlackIpPo::getIp,ip));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insertOne(E3BlackIpPo e3BlackIpPo) {
        blackIpMapper.insert(e3BlackIpPo);
    }
}
